<template>
  <ele-page>
    <!-- 搜索表单 -->
    <search @search="reload" />
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        highlight-current-row
        border
        stripe
        :where="defalutWhere"
      >
        <template #toolbar="{}">
          <el-button type="info" @click="exportExcel" :icon="DocumentCopy">
            导出表格
          </el-button>
        </template>
        <template #plate_number="{ row }">
          <span class="blue pointer" @click="openDetail(row)">{{
            row.plate_number
          }}</span>
        </template>
        <template #insurance_date_c="{ row }">
          <span class="blue pointer" @click="openInsurance(row, '交强险')">{{
            row.insurance_date_c
          }}</span>
        </template>
        <template #insurance_date_b="{ row }">
          <span class="blue pointer" @click="openInsurance(row, '商业险')">{{
            row.insurance_date_b
          }}</span>
        </template>
        <template #actionChoose="{ row }">
          <el-button type="info" size="small" @click="choose(row)">
            选择
          </el-button>
        </template>
        <template #action="{ row }">
          <!-- <span v-if="row.status === 9999">/</span>
          <el-button-group v-else>
            <el-button type="success" size="small" @click="openOffer(row)">
              报价申请
            </el-button>
            <el-button type="warning" size="small" @click="openEdit(row)"
              >编辑</el-button
            >

            <el-button type="danger" size="small" @click="del(row)"
              >删除</el-button
            >
          </el-button-group> -->

          <el-button type="primary" size="small"> 操作 </el-button>
        </template>
      </ele-pro-table>
    </ele-card>

    <!-- 车辆详情 -->
    <detail v-model="showDetail" :data="current" />
    <!-- 车辆保险信息 -->
    <insurance v-model="showInsurance" :data="current" :cate="currentCate" />
  </ele-page>
</template>

<script setup>
  import { ref } from 'vue';
  import Search from './components/search.vue';
  import {
    pageVehicleExpiredList,
    exportVehicleInsuranceExpired
  } from '@/api/company';
  import { EleMessage } from 'ele-admin-plus/es';

  import Detail from '../components/detail.vue';
  import Insurance from '../components/insurance.vue';

  const props = defineProps({
    /** 修改回显的数据 */
    action: {
      type: String,
      default: 'list'
    }
  });
  /** 表格实例 */
  const tableRef = ref(null);

  /** 选择车辆用表格列配置 */
  const columnsChoose = ref([
    {
      prop: 'id',
      label: 'ID',
      width: 85,
      align: 'center'
    },
    {
      width: 70,
      align: 'center',
      prop: 'area',
      label: '区域'
    },
    {
      width: 120,
      slot: 'plate_number',
      label: '车牌号码'
    },
    {
      prop: 'company',
      label: '车队'
    },
    {
      columnKey: 'actionChoose',
      label: '操作',
      slot: 'actionChoose',
      width: 100,
      align: 'center',
      fixed: 'right'
    }
  ]);
  /** 列表的列 */
  const columnsList = ref([
    {
      prop: 'id',
      label: 'ID',
      width: 85,
      align: 'center'
    },
    {
      width: 70,
      align: 'center',
      prop: 'area',
      label: '区域'
    },
    {
      width: 80,
      align: 'center',
      prop: 'status',
      label: '状态'
    },
    {
      width: 120,
      slot: 'plate_number',
      label: '车牌号码'
    },
    {
      width: 200,
      prop: 'company',
      label: '车队'
    },
    {
      width: 100,
      prop: 'realname',
      label: '车主'
    },
    {
      prop: 'tel',
      label: '联系电话'
    },
    {
      width: 120,
      slot: 'insurance_date_c',
      align: 'center',
      label: '交强险到期'
    },
    {
      width: 120,
      slot: 'insurance_date_b',
      align: 'center',
      label: '商业险到期'
    },

    {
      columnKey: 'action',
      label: '操作',
      slot: 'action',
      width: 100,
      align: 'center',
      fixed: 'right'
    }
  ]);

  const columns =
    props.action == 'list' ? columnsList.value : columnsChoose.value;

  /** 表格选中数据 */
  const selections = ref([]);

  /** 当前编辑数据 */
  const current = ref(null);

  const defalutWhere = ref({
    deadlineType: '交强险',
    status: '正常'
  });
  /** 表格数据源 */
  const datasource = ({ page, limit, where, orders }) => {
    return pageVehicleExpiredList({
      ...where,
      ...orders,
      page,
      limit
    });
  };

  /** 搜索 */
  const reload = (where) => {
    selections.value = [];
    tableRef.value?.reload?.({ page: 1, where });
  };

  /** 车辆详情 */
  const showDetail = ref(false);
  const openDetail = (row) => {
    current.value = row ?? null;
    showDetail.value = true;
  };

  /** 保险信息 */
  const showInsurance = ref(false);
  const currentCate = ref('全部');
  const openInsurance = (row, cate) => {
    current.value = row ?? null;
    currentCate.value = cate;
    showInsurance.value = true;
  };

  /** 导出表格 */
  const exportExcel = () => {
    const loading = EleMessage.loading('请求中..');
    tableRef.value?.fetch(({ where }) => {
      // 方法的参数与数据源的完全一致
      // 请求查询全部接口
      console.log(where);
      var requestData = where;
      requestData.limit = 99999999;
      requestData.page = 1;

      exportVehicleInsuranceExpired(requestData)
        .then((res) => {
          loading.close();
          window.open(res);
        })
        .catch(() => {
          loading.close();
        });
    });
  };

  /** 选择 */
  const emit = defineEmits(['done']);
  const choose = (row) => {
    emit('done', row);
  };
</script>
